#include <stdio.h>
#include <stdlib.h>
/**
 * 如果你的栈有头结点且头结点不存储有效数据,且top指向栈顶的有效数据,那么top->next == NULL表示栈空.
如果你的栈有头结点且头结点存储有效数据,且top指向栈顶的有效数据,那么top==NULL表示栈空.
 */
typedef char DataType; //假设结点的数据域类型是字符
typedef struct node { //结点类型定义
    DataType data; //结点的数据域
    struct node *next;//结点的指针域
} StackNode;
typedef StackNode *LinkStack;


//置空栈
void InitStack(LinkStack top)
{
    top->next = NULL;
}

//判栈空
int StackEmpty(LinkStack top)
{
    return top->next == NULL;
}

//进栈
LinkStack Push(LinkStack top, DataType x)
{
    StackNode *p;
    p = (StackNode *)malloc(sizeof(StackNode));
    p->data = x; // 数据域赋值 x
    p->next = top->next;//指针域指向栈顶
    top->next = p;//栈顶指向新结点
    return top;
}

//退栈
LinkStack Pop(LinkStack top, DataType *x)
{
    StackNode *p=top->next;

    if(StackEmpty(top)){
        printf("stack is empty");
        exit(0);
    }else{
        *x = p->data;
        top->next = p->next;
        free(p);
        return top;
    }
}

//取栈顶元素
DataType GetPop(LinkStack top)
{
    if(StackEmpty(top)){
        printf("stack is empty");
        exit(0);
    }
    else{
        return top->next->data;
    }
}

int main()
{
    LinkStack top;
    DataType x;
    top = (StackNode *)malloc(sizeof(StackNode));
    InitStack(top);
    Push(top, 'a');
    Push(top, 'b');
    Push(top, 'c');
//    putchar(GetPop(top));
    Pop(top, &x);
    putchar(x);
    Pop(top, &x);
    putchar(x);
    Pop(top, &x);
    putchar(x);

//    putchar(Pop(top));
//    putchar(Pop(top));
    return 0;
}